﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using DapperWrapper;
using ZLWeb.WebApiRepository.Base;
using ZilLion.Service.Core.Infrastructure.Base;
using ZilLion.Service.Core.DapperWrapper;
using Microsoft.AspNetCore.Mvc;

namespace ZLWeb.WebApiInfrastructure.Filter
{
    public class WebApiBase : ZilLionControllerBase
    {
        private IDbContext DbContext { get; set; }

        private IDbContext DbContext1 { get; set; }

        protected long? Accid { get; set; }

        protected string Passport { get; set; }

        protected string PassportType { get; set; }

        protected long? Accid1 { get; set; }

        protected string Passport1 { get; set; }

        protected string PassportType1 { get; set; }

        protected string Orgmembershipno { get; set; }

        protected string Orgmembershipno1 { get; set; }

        protected string Openid { get; set; }

        [NonAction]
        public void SetAccid(long? Accid)
        {
            this.Accid = Accid;
        }

        [NonAction]
        public void SetBaseInfo(long? Accid, string Passport, string PassportType,string wxcardno)
        {
            this.Accid = Accid;
            this.Passport = Passport;
            this.PassportType = PassportType;
            this.Orgmembershipno = wxcardno;
        }

        [NonAction]
        public void SetBaseInfo1(long? Accid, string Passport, string PassportType, string wxcardno1)
        {
            this.Accid1 = Accid;
            this.Passport1 = Passport;
            this.PassportType1 = PassportType;
            this.Orgmembershipno1 = wxcardno1;
        }

        [NonAction]
        public void SetOpenid(string Openid)
        {
            this.Openid = Openid;
        }

        [NonAction]
        public void SetDbContext(IDbContext _DbContext)
        {
            DbContext = _DbContext;
        }

        [NonAction]
        public void SetDbContext1(IDbContext _DbContext1)
        {
            DbContext1 = _DbContext1;
        }

        private NamedParameter _namedparameter;

        public NamedParameter Accidparameter
        {
            get
            {
                if (_namedparameter == null)
                {
                    _namedparameter = new NamedParameter("Accid", Accid);
                }
                return _namedparameter;
            }
        }

        private NamedParameter _namedparameter1;

        public NamedParameter Accidparameter1
        {
            get
            {
                if (_namedparameter1 == null)
                {
                    _namedparameter1 = new NamedParameter("Accid1", Accid1);
                }
                return _namedparameter1;
            }
        }

        private NamedParameter _dbContextparameter;

        public NamedParameter DBContextparameter
        {
            get
            {
                if (_dbContextparameter == null)
                {
                    _dbContextparameter = new NamedParameter("context", DbContext);
                }
                return _dbContextparameter;
            }
        }

        private NamedParameter _dbContextparameter1;

        public NamedParameter DBContextparameter1
        {
            get
            {
                if (_dbContextparameter1 == null)
                {
                    _dbContextparameter1 = new NamedParameter("context", DbContext1);
                }
                return _dbContextparameter1;
            }
        }

        private NamedParameter _DBConnparameter;

        public NamedParameter DBConnparameter
        {
            get
            {
                if (_DBConnparameter == null)
                {
                    if (PassportType == "-1")
                    {
                        string[] labels = Passport.Split('|');
                        string mikvname = labels[0];
                        string miidtype = labels[1];
                        string miid = labels[2];
                        var item = SqlConnectionLoader.ReadDbContextList.Cast<SourceContextInstance>()
                            .FirstOrDefault(tr => tr.mikvname == mikvname && tr.miidtype == miidtype && tr.miid.ToString() == miid);
                        _DBConnparameter = new NamedParameter("DBConn", item);
                    }
                }
                return _DBConnparameter;
            }
        }

        private NamedParameter _DbContextparameterparameter;

        public NamedParameter DbContextparameter
        {
            get
            {
                if (_DbContextparameterparameter == null)
                {
                    _DbContextparameterparameter = new NamedParameter("context", new SourceAContext());
                }
                return _DbContextparameterparameter;
            }
        }

        public enum PassportTypeCode
        {
            实体卡 = 1,
            微信appidopenid = 2,
            账户识别码 = 4,
            微信membershipno = 6
        }
    }
}
